package com.xq.recyclerviewlib;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/07.
 */

public abstract class CommonQuickAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected static final String TAG = CommonQuickAdapter.class.getSimpleName();
    protected Context mContext;
    protected int mLayoutResId;
    protected LayoutInflater mLayoutInflater;
    protected List<T> mData;

    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    private OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener;

    /**
     * View to show if there are no items to show.
     */
    private View emptyView;

    public static final int EMPTY_VIEW = 0x00000555;

    public CommonQuickAdapter(int mLayoutResId, List<T> mData) {
        this.mData = mData;
        if(mLayoutResId != 0){
            this.mLayoutResId = mLayoutResId;
        }
    }

    public CommonQuickAdapter(List<T> mData) {
        this(0,mData);
    }

    public View getEmptyView() {
        return emptyView;
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CommonViewHolder commonViewHolder = null;
        if(mContext == null){
            this.mContext = parent.getContext();
        }

        if(mLayoutInflater == null){
            this.mLayoutInflater = LayoutInflater.from(mContext);
        }

        switch (viewType){
            case EMPTY_VIEW:
                commonViewHolder = new CommonViewHolder(emptyView);
                break;
            default:
                commonViewHolder = onCreateDefViewHolder(parent,viewType);
                initItemClickListener(commonViewHolder);
                break;
        }
        return commonViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = holder.getItemViewType();
        switch (viewType){
            case EMPTY_VIEW:
                break;
            default:
                convert((CommonViewHolder) holder,mData.get(position),position);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(mData.size() == 0 && position == 0){
            return EMPTY_VIEW;
        }else{
            return getDefItemViewType(position);
        }
    }

    private void initItemClickListener(final CommonViewHolder commonViewHolder){
        commonViewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(onRecyclerViewItemClickListener != null){
                    onRecyclerViewItemClickListener.onItemClick(v,commonViewHolder.getLayoutPosition());
                }
            }
        });

        commonViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {
                if(onRecyclerViewItemLongClickListener != null){
                    return onRecyclerViewItemLongClickListener.onItemLongClick(v,commonViewHolder.getLayoutPosition());
                }
                return false;
            }
        });
    }
    protected CommonViewHolder onCreateDefViewHolder(ViewGroup parent,int viewType){
        return createBaseViewHolser(parent,mLayoutResId);
    }

    protected CommonViewHolder createBaseViewHolser(ViewGroup parent,int layoutResId){
        return new CommonViewHolder(getItemView(layoutResId,parent));
    }

    /**
     * @param layoutResId ID for an XML layout resource to load
     * @param parent      Optional view to be the parent of the generated hierarchy or else simply an object that
     *                    provides a set of LayoutParams values for root of the returned
     *                    hierarchy
     * @return view will be return
     */
    protected View getItemView(int layoutResId, ViewGroup parent) {
        return mLayoutInflater.inflate(layoutResId, parent, false);
    }

    protected int getDefItemViewType(int position) {
        return super.getItemViewType(position);
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param commonViewHolder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    protected abstract void convert(CommonViewHolder commonViewHolder, T item,int position);

    @Override
    public int getItemCount() {
        int count = mData == null ? 0 : mData.size();
        if(mData.size() == 0 && emptyView != null){
            count += 1;
        }
        return count;
    }

    public List<T> getData() {
        return mData;
    }

    public void setData(List<T> mData) {
        this.mData = mData;
    }

    /**
     * if mEmptyView will be return 1 or not will be return 0
     *
     * @return
     */
    public int getEmptyViewCount() {
        return emptyView == null ? 0 : 1;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public OnRecyclerViewItemLongClickListener getOnRecyclerViewItemLongClickListener() {
        return onRecyclerViewItemLongClickListener;
    }

    public void setOnRecyclerViewItemLongClickListener(OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener) {
        this.onRecyclerViewItemLongClickListener = onRecyclerViewItemLongClickListener;
    }

    /**
     * Interface definition for a callback to be invoked when an item in this
     * AdapterView has been clicked.
     */
    public interface OnRecyclerViewItemClickListener {
        /**
         * Callback method to be invoked when an item in this AdapterView has
         * been clicked.
         *
         * @param view     The view within the AdapterView that was clicked (this
         *                 will be a view provided by the adapter)
         * @param position The position of the view in the adapter.
         */
        public void onItemClick(View view, int position);
    }

    /**
     * Interface definition for a callback to be invoked when an item in this
     * view has been clicked and held
     */
    public interface OnRecyclerViewItemLongClickListener {
        /**
         * callback method to be invoked when an item in this view has been
         * click and held
         *
         * @param view     The view whihin the AbsListView that was clicked
         * @param position The position of the view int the adapter
         * @return true if the callback consumed the long click ,false otherwise
         */
        public boolean onItemLongClick(View view, int position);
    }
}
